/*
 * 对话消息框
 */

#include "gui.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int msgbox_init()
{

	return 0;
}

void ACALLBACK _msg_panel_render(struct Panel *pn, void *userdata)
{
	struct Msgbox *msg = (struct Msgbox *) userdata;
	
	int px, py, w, h;
	panel_get_abs_pos(pn, &px, &py);
	panel_get_size(pn, &w, &h);
	
	//框架
	video_drawimg_x(gui_get_box_bg(), 0, 0, w, h, px, py);
	video_set_color(0, 0, 250);
	video_rectangle(px, py, px + w, py + h);
	
	
	//标题
	video_drawimg(msg -> _img_title,
		      px + (w - img_get_width(msg -> _img_title)) / 2,
		      py + 5);

	//信息
	int i;
	int dy = img_get_height(msg -> _img_msg[0]);
	for (i = 0; i < msg -> _msg_line; ++i) {
		video_drawimg(msg -> _img_msg[i], px + 100,
			      py + 50 + i * dy);
	}
}

void ACALLBACK bt_click_ok(struct Button *bt, void *userdata)
{
	struct Msgbox *msg = (struct Msgbox *) userdata;
	msg -> ret_val = MSG_OK;
}
void ACALLBACK bt_click_yes(struct Button *bt, void *userdata)
{
	struct Msgbox *msg = (struct Msgbox *) userdata;
	msg -> ret_val = MSG_YES;
}
void ACALLBACK bt_click_no(struct Button *bt, void *userdata)
{
	struct Msgbox *msg = (struct Msgbox *) userdata;
	msg -> ret_val = MSG_NO;
}
void ACALLBACK bt_click_cancel(struct Button *bt, void *userdata)
{
	struct Msgbox *msg = (struct Msgbox *) userdata;
	msg -> ret_val = MSG_CANCEL;
}

void ACALLBACK _msg_panel_down(struct Panel *pn,
				 int button, int x, int y, void *userdata)
{
	struct Msgbox *msg = (struct Msgbox *) userdata;
	msg -> button_flag = 1;
	msg -> old_x = x;
	msg -> old_y = y;
}

void ACALLBACK _msg_panel_up(struct Panel *pn,
			       int button, int x, int y, void *userdata)
{
	struct Msgbox *msg = (struct Msgbox *) userdata;
	msg -> button_flag = 0;
}

void ACALLBACK _msg_panel_move(struct Panel *pn, int x, int y, void *userdata)
{
	struct Msgbox *msg = (struct Msgbox *) userdata;
	if (msg -> button_flag) {
		int px, py;
		panel_get_pos(pn, &px, &py);
		panel_set_pos(pn, px + (x - msg -> old_x), py + (y - msg -> old_y));
	}
}


//得到行数
int get_line_count(char *a)
{
	int line = 1;
	while(*a) {
		if (*a == '\n') line++;
		a++;
	}
	return line;
}

struct Msgbox *msgbox_new(char *title, char *msg, int type, int button)
{
	struct Msgbox *ret = malloc(sizeof(struct Msgbox));
	memset(ret, 0, sizeof(struct Msgbox));
	
	//得到行数, 并为每一行申请图片空间
	char buf[4096];
	if (msg == NULL) msg = " ";
	ret -> _msg_line = get_line_count(msg);
	ret -> _img_msg = malloc(ret -> _msg_line * (sizeof(Image)));
	
	//开始为每一行创建图片
	int i = 0;
	int cline = 0;
	while(1) {
		buf[i] = *msg;
		if (buf[i] == '\n' || buf[i] == 0) {
			buf[i] = 0;
			//printf("%d: %s\n", cline, buf);
			ret -> _img_msg[cline] = ttf_make_img(gui_get_font_small(), 
					buf, 0);
			i = 0;
			cline++;
			if (*msg == 0) break;
		} else i++;
		msg++;
	}
	
	//寻找最大宽度
	int w, wmax = 0;
	for (i = 0; i < ret -> _msg_line; ++i) {
		w = img_get_width(ret -> _img_msg[i]);
		if (w > wmax) wmax = w;
	}
	
	//计算所需面板宽高
	int panel_w, panel_h;
	panel_w = wmax + 120;
	if (panel_w < 300) panel_w = 300;	//最小300
	
	panel_h = img_get_height(ret -> _img_msg[0]) * ret -> _msg_line + 100;
	if (panel_h < 160) panel_h = 160;	//最小150
	
	//从根创建面板, 居中处理
	ret -> pn_window = panel_new(NULL, (800 - panel_w) / 2,
				     (600 - panel_h) / 2, panel_w, panel_h);
	
	//生成标题图片
	if (title == NULL) title = " ";
	ret -> _img_title =  ttf_make_img(gui_get_font_big(), title, 0);
	
	//生成按钮
	//统计按钮个数
	int bt_cnt = 0;
	if(button & MSG_OK) bt_cnt++;
	if(button & MSG_YES) bt_cnt++;
	if(button & MSG_NO) bt_cnt++;
	if(button & MSG_CANCEL) bt_cnt++;
	
	int bt_x = (panel_w - (bt_cnt * 65)) / 2;	//每个按钮60,高30

	if ((button & MSG_OK)) {
		ret -> bt_ok = button_new(ret -> pn_window, bt_x, panel_h - 40, 60, 30);
		button_set_caption(ret -> bt_ok, "OK");
		bt_x += 65;
		button_clicked(ret -> bt_ok, bt_click_ok);
		button_set_userdata(ret -> bt_ok, ret);
	}
	if ((button & MSG_YES)) {
		ret -> bt_yes = button_new(ret -> pn_window, bt_x, panel_h - 40, 60, 30);
		button_set_caption(ret -> bt_yes, "YES");
		bt_x += 65;
		button_clicked(ret -> bt_yes, bt_click_yes);
		button_set_userdata(ret -> bt_yes, ret);
	}
	if ((button & MSG_NO)) {
		ret -> bt_no = button_new(ret -> pn_window, bt_x, panel_h - 40, 60, 30);
		button_set_caption(ret -> bt_no, "NO");
		bt_x += 65;
		button_clicked(ret -> bt_no, bt_click_no);
		button_set_userdata(ret -> bt_no, ret);
	}
	if ((button & MSG_CANCEL)) {
		ret -> bt_cancel = button_new(ret -> pn_window, bt_x, panel_h - 40, 80, 30);
		button_set_caption(ret -> bt_cancel, "CANCEL");
		bt_x += 65;
		button_clicked(ret -> bt_cancel, bt_click_cancel);
		button_set_userdata(ret -> bt_cancel, ret);
	}
	ret -> _button = button;
	
	panel_user_render(ret -> pn_window, _msg_panel_render);
	panel_set_userdata(ret -> pn_window, ret);
	panel_mouse_down(ret -> pn_window, _msg_panel_down);
	panel_mouse_up(ret -> pn_window, _msg_panel_up);
	panel_mouse_move(ret -> pn_window, _msg_panel_move);
	
	//TODO:处理type生成图标
	return ret;
}

void msgbox_delete(struct Msgbox *win)
{
	if ((win -> _button & MSG_OK))
		button_delete(win -> bt_ok);
	if ((win -> _button & MSG_YES))
		button_delete(win -> bt_yes);
	if ((win -> _button & MSG_NO))
		button_delete(win -> bt_no);
	if ((win -> _button & MSG_CANCEL))
		button_delete(win -> bt_cancel);
	
	if (win -> _img_title) video_destory_img(win -> _img_title);
	panel_delete(NULL, win -> pn_window);
	int i;
	for (i = 0; i < win -> _msg_line; ++i) {
		if (win -> _img_msg[i])
			video_destory_img(win -> _img_msg[i]);
	}
	free(win -> _img_msg);
	free(win);
}

//运行该对话框直到对话框返回
int msgbox_run(struct Msgbox *win)
{
	while (win -> ret_val == 0) {
		gui_event_idle(win -> pn_window -> _area);
		gui_render();
		SDL_Delay(10);
	}
	int ret = win -> ret_val;
	win -> ret_val = 0;
	return ret;
}

